package com.example.samplehelloandroid3;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements View.OnClickListener{
	TextView showTextView;
	long sum;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		showTextView = (TextView)findViewById(R.id.showText);
		int[] btns= {R.id.button0, R.id.button1, R.id.button2, R.id.button3, R.id.button4, R.id.button5, R.id.button6, 
				R.id.button7, R.id.button8, R.id.button9};
		for (int i= 0; i<btns.length; i++){
			((Button)findViewById(btns[i])).setOnClickListener(this);
		}
		((Button)findViewById(R.id.button_plus)).setOnClickListener(this);
		((Button)findViewById(R.id.button_equal)).setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		Button btn = (Button)v;
		if (btn.getText().toString().equals("+")){
			String result= showTextView.getText().toString();
			if (result.equals("")) 
				result="0";
			sum +=Integer.parseInt(result);
			showTextView.setText("");
		}
		else if (btn.getText().toString().equals("=")){
			String result= showTextView.getText().toString();
			if (result.equals("")) 
				result="0";
			sum +=Integer.parseInt(result);			
			showTextView.setText(String.valueOf(sum));
			sum= 0;
		} else {
			showTextView.setText(showTextView.getText()+ btn.getText().toString());
		}
	}

}
